package com.loe.logger

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.BaseAdapter
import android.widget.TextView
import com.loe.logger.db.LoggerSharedManager
import org.json.JSONArray
import org.json.JSONObject

class LoggerTestAdapter(private val context: Context) : BaseAdapter()
{
    class Bean
    {
        var url: String
        var name: String

        constructor(json: JSONObject)
        {
            this.name = json.optString("name")
            this.url = json.optString("url")
        }

        constructor(name: String, url: String)
        {
            this.name = name
            this.url = url
        }

        public fun toJson(): JSONObject
        {
            val json = JSONObject()
            json.put("name", name)
            json.put("url", url)
            return json
        }
    }

    private var cacheData: String
        get() = LoggerSharedManager.getString("logger:testCacheData")
        set(value)
        {
            LoggerSharedManager.putString("logger:testCacheData", value)
        }

    private val list: ArrayList<Bean> by lazy()
    {
        val list = ArrayList<Bean>()
        try
        {
            val js = JSONArray(cacheData)
            for (i in 0 until js.length())
            {
                val json = js.optJSONObject(i)
                val bean = Bean(json)
                list.add(bean)
            }
        } catch (e: Exception)
        {
        }
        list
    }

    private fun saveCacheData()
    {
        val js = JSONArray()
        for (bean in list)
        {
            js.put(bean.toJson())
        }
        cacheData = js.toString()
    }

    fun addBean(name: String, url: String)
    {
        list.add(Bean(name, url))
        notifyDataSetChanged()
        saveCacheData()
    }

    private val inflater: LayoutInflater = LayoutInflater.from(context)

    private var onItemClickListener: AdapterView.OnItemClickListener? = null

    override fun getCount(): Int
    {
        return list.size
    }

    override fun getItem(position: Int): Any
    {
        return list[position]
    }

    override fun getItemId(position: Int): Long
    {
        return position.toLong()
    }

    override fun getView(position: Int, view: View?, viewGroup: ViewGroup): View
    {
        var view = view
        val bean = list[position]

        val item: Item
        if (view == null)
        {
            item = Item()
            // 获取Item布局构造view
            view = inflater.inflate(R.layout.logger_test_item, null)
            // 获取布局子元素
            item.textName = view!!.findViewById(R.id.textName) as TextView
            item.textUrl = view!!.findViewById(R.id.textUrl) as TextView
            item.buttonDelete = view!!.findViewById(R.id.buttonDelete) as TextView
            // 将Item绑定到view里
            view.tag = item
        } else
        {
            // 如果view已被构造，则直接获取绑定Item
            item = view.tag as Item
        }
        // 显示
        item.textName?.text = bean.name
        item.textUrl?.text = bean.url
        item.buttonDelete?.setOnClickListener()
        {
            list.remove(bean)
            notifyDataSetChanged()
            saveCacheData()
        }
        return view
    }

    internal inner class Item
    {
        var textName: TextView? = null
        var textUrl: TextView? = null
        var buttonDelete: TextView? = null
    }
}